package avicit.bdp.dds.service.permission;

import avicit.bdp.dds.dispatch.enums.AuthorizationType;
import avicit.bdp.dds.dispatch.process.ResourceInfo;
import avicit.bdp.dds.service.process.ProcessService;
import org.slf4j.Logger;

import java.util.List;

public class PermissionCheck<T> {
    /**
     * logger
     */
    private Logger logger;
    /**
     * Authorization Type
     */
    private AuthorizationType authorizationType;

    /**
     * Authorization Type
     */
    private ProcessService processService;

    /**
     * need check array
     */
    private T[] needChecks;

    /**
     * resoruce info
     */
    private List<ResourceInfo> resourceList;

    /**
     * user id
     */
    private String userId;

    /**
     * permission check
     * @param authorizationType authorization type
     * @param processService        process dao
     */
    public PermissionCheck(AuthorizationType authorizationType, ProcessService processService) {
        this.authorizationType = authorizationType;
        this.processService = processService;
    }

    /**
     * permission check
     * @param authorizationType
     * @param processService
     * @param needChecks
     * @param userId
     */
    public PermissionCheck(AuthorizationType authorizationType, ProcessService processService, T[] needChecks, String userId) {
        this.authorizationType = authorizationType;
        this.processService = processService;
        this.needChecks = needChecks;
        this.userId = userId;
    }

    /**
     * permission check
     * @param authorizationType
     * @param processService
     * @param needChecks
     * @param userId
     * @param logger
     */
    public PermissionCheck(AuthorizationType authorizationType, ProcessService processService, T[] needChecks, String userId, Logger logger) {
        this.authorizationType = authorizationType;
        this.processService = processService;
        this.needChecks = needChecks;
        this.userId = userId;
        this.logger = logger;
    }

    /**
     * permission check
     * @param logger
     * @param authorizationType
     * @param processService
     * @param resourceList
     * @param userId
     */
    public PermissionCheck(AuthorizationType authorizationType, ProcessService processService, List<ResourceInfo> resourceList, String userId,Logger logger) {
        this.authorizationType = authorizationType;
        this.processService = processService;
        this.resourceList = resourceList;
        this.userId = userId;
        this.logger = logger;
    }

    public AuthorizationType getAuthorizationType() {
        return authorizationType;
    }

    public void setAuthorizationType(AuthorizationType authorizationType) {
        this.authorizationType = authorizationType;
    }

    public ProcessService getProcessService() {
        return processService;
    }

    public void setProcessService(ProcessService processService) {
        this.processService = processService;
    }

    public T[] getNeedChecks() {
        return needChecks;
    }

    public void setNeedChecks(T[] needChecks) {
        this.needChecks = needChecks;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public List<ResourceInfo> getResourceList() {
        return resourceList;
    }

    public void setResourceList(List<ResourceInfo> resourceList) {
        this.resourceList = resourceList;
    }

    /**
     * has permission
     * @return true if has permission
     */
    public boolean hasPermission(){
        try {
            checkPermission();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * check permission
     * @throws Exception exception
     */
    public void checkPermission() throws Exception{
        if(this.needChecks.length > 0){

            // get user type in order to judge whether the user is admin
//            User user = processService.getUserById(userId);
//            if (user == null) {
//                logger.error("user id {} didn't exist",userId);
//                throw new RuntimeException(String.format("user %s didn't exist",userId));
//            }
//            if (user.getUserType() != UserType.ADMIN_USER){
//                List<T> unauthorizedList = processService.listUnauthorized(userId,needChecks,authorizationType);
//                // if exist unauthorized resource
//                if(CollectionUtils.isNotEmpty(unauthorizedList)){
//                    logger.error("user {} didn't has permission of {}: {}", user.getUserName(), authorizationType.getDescp(),unauthorizedList);
//                    throw new RuntimeException(String.format("user %s didn't has permission of %s %s", user.getUserName(), authorizationType.getDescp(), unauthorizedList.get(0)));
//                }
//            }
        }
    }

}
